package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.Broadcast;
import com.leoman.service.BroadcastService;
import com.leoman.service.CommentService;
import com.leoman.service.PraiseService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 微音管理
 * Created by 涂奕恒 on 2016/12/12.
 */
@Controller
@RequestMapping(value = "backend/broadcast")
public class BroadcastController extends CommonController {

    @Autowired
    private BroadcastService broadcastService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private PraiseService praiseService;

    @RequestMapping("index")
    public String index() {
        return "backend/微音列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String userName,
                     String startTime,
                     String endTime,
                     Integer isAdmin,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Broadcast> page = broadcastService.pageByParams(userName, startTime, endTime, isAdmin, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("add")
    public String add(ModelMap model, Integer broadcastId) {
        if (null != broadcastId) {
            Broadcast broadcast = broadcastService.getById(broadcastId);
            broadcast.setCommentNum(commentService.findListBySourceIdAndType(broadcastId, Constant.SOURCE_TYPE_BROADCAST).size());
            broadcast.setPraiseNum(praiseService.findListBySourceIdAndType(broadcastId, Constant.SOURCE_TYPE_BROADCAST).size());
            model.addAttribute("broadcast", broadcast);
            model.addAttribute("duration", getTimeByDuration(broadcast.getDuration()));
            model.addAttribute("createTime", DateUtils.longToStringWithFormat(broadcast.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));

            return "backend/微音详情";
        } else {
            return "backend/添加微音";
        }
    }

    private String getTimeByDuration(Integer duration) {
        if (duration > 60) {
            return (duration / 60) + "分" + (duration % 60) + "秒";
        } else if (duration == 60) {
            return "1分";
        } else {
            return duration + "秒";
        }
    }

    /**
     * 置顶微音
     *
     * @param broadcastId 微音ID
     */
    @RequestMapping("/toTop")
    @ResponseBody
    public Integer toTop(Integer broadcastId) {
        try {
            return broadcastService.toTop(broadcastId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加or修改微音
     */
    @RequestMapping("/save")
    @ResponseBody
    public Integer save(String title, MultipartRequest multipartRequest) {
        try {
            return broadcastService.saveBroadcast(title, multipartRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除微音
     *
     * @param broadcastId 微音ID
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer broadcastId) {
        try {
            broadcastService.deleteById(broadcastId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除微音
     *
     * @param ids 微音ID
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Integer batchDelete(String ids) {
        try {
            return broadcastService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
